package com.boot.unity.api.modules.train.service.impl;

import com.boot.unity.api.modules.train.dao.StuDao;
import com.boot.unity.api.modules.train.dao.StudentDao;
import com.boot.unity.api.modules.train.entity.Stu;
import com.boot.unity.api.modules.train.entity.Student;
import com.boot.unity.api.modules.train.service.StudentUpdateDataService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
@RequiredArgsConstructor
public class StudentUpdateDataServiceImpl implements StudentUpdateDataService {
    private final StuDao stuDao;
    private final StudentDao studentDao;


    @Override
    public boolean upStudentForStatus(String studentId) {
        Student entity = studentDao.getByStudentId(studentId);
        //更新报名学员和无纸化之间的学员ID的关系
        Stu stuEntity = stuDao.queryStuPayOkNoTf(entity.getCardNo(), entity.getTrainType());
        if(stuEntity == null){
            return true;
        }
        if(stuEntity != null){
            if(StringUtils.isEmpty(stuEntity.getStudentId())){
                stuDao.updateStudentIdById(studentId,stuEntity.getId()+"");
            }

            //如果是退费的学员，检索报名状态，处理计时的推送
            String status = entity.getStatus();
            if(!StringUtils.isEmpty(status)){
                if(status.trim().equals("2")){
                    String cardNo = entity.getCardNo();
                    String trainType = entity.getTrainType();

                    List<Stu> list = stuDao.queryPayOkNoTf(cardNo, trainType);
                    if(list.size() > 0){//存在又缴费的情况
                        studentDao.updateStatusByStudentId(studentId,0);
                    }
                }
            }
        }





        //更新学员信息(当学员信息没有录入或者录入错误的时候，进行学员信息的订正)
        Stu stu = stuDao.getByCardNo(entity.getCardNo(), entity.getCardType());
        if(entity != null){
            if(stu != null){
                boolean flag = false;
                if(StringUtils.isEmpty(stu.getName())){
                    flag = true;
                }else{
                    if(!stu.getName().equals(entity.getName())){
                        flag = true;
                    }
                }

                if(StringUtils.isEmpty(stu.getGender())){
                    flag = true;
                }else{
                    if(!stu.getGender().equals(entity.getGender()+"")){
                        flag = true;
                    }
                }

                if(StringUtils.isEmpty(stu.getAddress())){
                    flag = true;
                }else{
                    if(!stu.getAddress().equals(entity.getAddress())){
                        flag = true;
                    }
                }

                if(StringUtils.isEmpty(stu.getMobile())){
                    flag = true;
                }else{
                    if(!stu.getMobile().equals(entity.getMobile())){
                        flag = true;
                    }
                }


                if(flag){
                    stu.setName(entity.getName());
                    stu.setGender(entity.getGender()+"");
                    stu.setAddress(entity.getAddress());
                    stu.setNationality(entity.getNationality());
                    stu.setMobile(entity.getMobile());
                    stu.setNation(entity.getNation());
                    stu.setLearningCategories(entity.getLearningCategories());
                    stuDao.updateStu(stu);
                    stuDao.updatePayInfoName(entity.getCardNo(),entity.getName());
                }
            }
        }

        return true;
    }
}
